package de.lulusoft.anothertorrent.gui.commands;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;

import de.lulusoft.anothertorrent.core.rtorrent.Torrent;
import de.lulusoft.anothertorrent.gui.exceptions.ExceptionHandler;

public class StopTorrentCommand implements IHandler {
	
	public static final String ID = "de.lulusoft.anothertorrent.gui.commands.StopTorrentCommand";

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		if (event.getTrigger() instanceof Torrent)
		{
			Torrent t = (Torrent)event.getTrigger();
			try {
				t.getServer().getRTorrentManager().torrentStop(t.getHash());
				t.setState(1L);
			} catch (Exception e) {
				ExceptionHandler.alert("Could not stop torrent", e);
			}			
		}		
		return null;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public boolean isHandled() {
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
	}

}
